package cn.myapp.ipc;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity implements OnClickListener {
	private InnerServiceConnection conn;
	private IMusicPlayer player;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		conn = new InnerServiceConnection();
		int flags = BIND_AUTO_CREATE;
		Intent service = new Intent(this, WorkService.class);
		bindService(service, conn, flags);

		findViewById(R.id.btn_play).setOnClickListener(this);
		findViewById(R.id.btn_pause).setOnClickListener(this);
		findViewById(R.id.btn_get_current_position).setOnClickListener(this);
		findViewById(R.id.btn_get_duration).setOnClickListener(this);
		findViewById(R.id.btn_is_playing).setOnClickListener(this);
	}

	private class InnerServiceConnection implements ServiceConnection {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			player = (IMusicPlayer) service;
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub

		}

	}

	@Override
	public void onClick(View v) {
//		switch (v.getId()) {
//		case R.id.btn_play:
//			player.play();
//			break;
//		case R.id.btn_pause:
//			player.pause();
//			break;
//		case R.id.btn_get_current_position:
//			player.getCurrentPosition();
//			break;
//		case R.id.btn_get_duration:
//			player.getDuration();
//			break;
//		case R.id.btn_is_playing:
//			player.isPlaying();
//			break;
//		}
	}
	
	@Override
	protected void onDestroy() {
		unbindService(conn);
		
		super.onDestroy();
	}

}
